<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Component TodoList</title>
</head>
<style>
    li {
        margin:5px 0;
        padding:2px 0;
        cursor: pointer;
    }
</style>
<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button v-on:click="handleBtnClick">提交</button>
        <p style="color:purple">点击列表删除</p>
        <ul>
            <!-- <li v-for="(item, index) in list">{{item}}---{{index}}</li> -->
            <todo-item 
            :content="item"
            :index="index"
            v-for="(item, index) in list"
            @delete="handleItemDelete">
            </todo-item>
        </ul>
    </div>
</body>
<script src="vue.js"></script>
<script>
    // 全局组件
    /* Vue.component('TodoItem', {
        props: ['content'],
        template: '<li>{{content}}</li>'
    }) */
    // 局部组件
    var TodoItem = {
        props: ['content', 'index'],
        data: function() {
            return {
                idx: this.index
            }
        },
        template: `<li>{{content}} <button @click="handleItemClick"z>删除</button></li>`,
        methods: {
            handleItemClick: function() {
                this.$emit('delete', this.idx)
            }
        }
    }
    var app = new Vue({
        el: '#app',
        data: {
            list: ['第一课的内容', '第二课的内容', '第三课的内容'],
            inputValue: ''
        },
        methods: {
            handleBtnClick: function () {
                this.list.push(this.inputValue);
                this.inputValue = '';
            },
            handleItemDelete: function(index) {
                console.log(index)
                this.list.splice(index,1);
            }
        },
        components: {
            TodoItem: TodoItem
        }
    })
</script>
</html>